﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Device.Location;
using Curling.BingMaps;
using System.IO.IsolatedStorage;

namespace Curling
{
    public static class LocationManager
    {
        readonly static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        static GeoCoordinateWatcher geoCoordinateWatcher;

        public static string Location = "";

        public static void FindMyCountry()
        {
            if (!settings.Contains("Location"))
            {
                geoCoordinateWatcher = new GeoCoordinateWatcher();
                geoCoordinateWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(geoCoordinateWatcher_PositionChanged);
                geoCoordinateWatcher.Start();
            }
        }

        private static void geoCoordinateWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            GeoCoordinate Location = e.Position.Location;

            if (geoCoordinateWatcher != null)
            {
                geoCoordinateWatcher.Stop();
                geoCoordinateWatcher.Dispose();
                geoCoordinateWatcher = null;
            }

            ReverseGeocode(Location);
        }

        private static void ReverseGeocode(GeoCoordinate location)
        {
            if (location == null)
                return;

            ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();

            // Set the credentials using a valid Bing Maps key
            reverseGeocodeRequest.Credentials = new BingMaps.Credentials();
            reverseGeocodeRequest.Credentials.ApplicationId = "ApxvAK17912OZfn0N6qH5c_g0YsdHgfCn6WKgrepucyW9PMRTHXzxAZWAfnCNjI7";

            // Set the point to use to find a matching address
            BingMaps.Location point = new BingMaps.Location();
            point.Latitude = location.Latitude;
            point.Longitude = location.Longitude;

            reverseGeocodeRequest.Location = point;

            // Make the reverse geocode request
            GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
            geocodeService.ReverseGeocodeCompleted += new EventHandler<ReverseGeocodeCompletedEventArgs>(geocodeService_ReverseGeocodeCompleted);
            geocodeService.ReverseGeocodeAsync(reverseGeocodeRequest);
        }

        private static void geocodeService_ReverseGeocodeCompleted(object sender, ReverseGeocodeCompletedEventArgs e)
        {
            Location = e.Result.Results[0].Address.CountryRegion;
            settings["Location"] = Location;
            settings.Save();
        }
    }
}
